package com.yzt.zhuju.widget.chatroom;

import android.content.Context;
import android.text.Layout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.yzt.zhuju.R;
import com.yzt.zhuju.network.entity.chatroom.MountsInfo;
import com.yzt.zhuju.utils.SvgaUtils;
import com.yzt.p_base.utils.DisplayUtils;
import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGADrawable;
import com.opensource.svgaplayer.SVGADynamicEntity;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * author : bob
 * date   : 2022/12/5
 * desc   :
 */
public class EnterChatRoomAnimView extends FrameLayout {

    private boolean isRunning = false;

    private TextView tvInfo;
    private FrameLayout flContainer;

    private SVGAImageView svgaImageView;
    private String svgaImage;
    private TranslateAnimation enterTranslateAnimation;
    private String svgaConfig;

    public EnterChatRoomAnimView(Context context) {
        this(context, null);
    }

    public EnterChatRoomAnimView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EnterChatRoomAnimView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_enter_chat_room, null);
        tvInfo = view.findViewById(R.id.info);
        flContainer = view.findViewById(R.id.fl_enter_container);
        addView(view);
    }

    public void showAnim(MountsInfo mountsInfo) {
        isRunning = true;
        svgaImage = mountsInfo.getUrl();
        svgaConfig = mountsInfo.getSvgaConfig();
        if (TextUtils.isEmpty(svgaConfig)) {
            tvInfo.setText(mountsInfo.getMessage());
            startTranslateAnim(tvInfo);
        } else {
            tvInfo.setVisibility(GONE);
        }
        startSpecialAnim();
    }

    /**
     * 平移动画
     */
    private void startTranslateAnim(View view) {
        if (view == null) {
            return;
        }
        tvInfo.setVisibility(VISIBLE);
        enterTranslateAnimation = new TranslateAnimation(DisplayUtils.getDisplayPxWidth(), 0, 0, 0);
        enterTranslateAnimation.setDuration(500);
        enterTranslateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        enterTranslateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.postDelayed(() -> tvInfo.setVisibility(GONE), 1000);
            }
        });
        view.startAnimation(enterTranslateAnimation);
    }

    /**
     * SVGA动画
     */
    private void startSpecialAnim() {
        //特殊动画
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.CENTER;
        flContainer.addView(initSpecialAnimView(), layoutParams);
    }

    /**
     * 全屏特殊动画
     */
    private SVGAImageView initSpecialAnimView() {
        svgaImageView = new SVGAImageView(getContext());
        LayoutParams layoutParams = new LayoutParams(DisplayUtils.getDisplayPxWidth(), DisplayUtils.getDisplayPxHeight());
        svgaImageView.setLayoutParams(layoutParams);
        svgaImageView.setLoops(1);
        svgaImageView.setClearsAfterStop(true);
        svgaImageView.setCallback(new SVGACallback() {
            @Override
            public void onStep(int i, double v) {
            }

            @Override
            public void onRepeat() {
            }

            @Override
            public void onFinished() {
                if (flContainer != null) {
                    flContainer.removeAllViews();
                }
                isRunning = false;
            }

            @Override
            public void onPause() {
            }
        });

        SVGAParser svgaParser = new SVGAParser(getContext());
        if (!TextUtils.isEmpty(svgaImage) && !"null".equals(svgaImage)) {
            URL url;
            try {
                Log.d("anim_svga_url-进房", svgaImage);
                url = new URL(svgaImage);
                svgaParser.parse(url, getParseCompletion(svgaImageView));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        return svgaImageView;
    }

    @NonNull
    private SVGAParser.ParseCompletion getParseCompletion(SVGAImageView svgaImageView) {
        return new SVGAParser.ParseCompletion() {
            @Override
            public void onComplete(SVGAVideoEntity svgaVideoEntity) {
                SVGADynamicEntity svgaDynamicEntity = SvgaUtils.requestDynamicItem(svgaConfig, Layout.Alignment.ALIGN_NORMAL);
                if (null != svgaDynamicEntity) {
                    SVGADrawable drawable = new SVGADrawable(svgaVideoEntity, svgaDynamicEntity);
                    svgaImageView.setImageDrawable(drawable);
                } else {
                    svgaImageView.setVideoItem(svgaVideoEntity);
                }
                svgaImageView.startAnimation();
            }

            @Override
            public void onError() {
                if (flContainer != null) {
                    flContainer.removeAllViews();
                }
                isRunning = false;
            }
        };
    }

    /**
     * 是否正在播放动画
     */
    public boolean isRunning() {
        return isRunning;
    }

    /**
     * 销毁动画
     */
    public void destroyAnim() {
        if (svgaImageView != null) {
            svgaImageView.stopAnimation();
        }
        if (enterTranslateAnimation != null) {
            enterTranslateAnimation.cancel();
        }
    }
}
